package com.bdqn.t382.controller;

import com.bdqn.t382.pojo.BookManage;
import com.bdqn.t382.service.BookManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/bookManage")
public class BookManageController {
    @Autowired
    BookManageService bookManageService;

    @RequestMapping("/listBook")
    public String list(Model model) {
        List<BookManage> listBook = bookManageService.getListBiz(new BookManage());
        model.addAttribute("listBook", listBook);
        return "listBook";
    }

    @GetMapping("/deleteBiz")
    public String deleteBiz(Integer id) {
        int biz = bookManageService.deleteBiz(id);
        if (biz > 0) {
            return "redirect:/bookManage/listBook";
        } else {
            return "redirect:/error.jsp";
        }
    }

    @PostMapping("/addBiz")
    public String addBiz(BookManage bookManage) {
        int insertBiz = bookManageService.insertBiz(bookManage);
        if (insertBiz > 0) {
            return "redirect:/bookManage/listBook";
        } else {
            return "redirect:/error.jsp";
        }
    }

    @RequestMapping("/addBook")
    public String addBook() {
        return "addBook";
    }

    @GetMapping("/viewBook")
    public String viewBook(Integer id, Model model) {
        BookManage bookManage = bookManageService.getByIdBiz(id);
        model.addAttribute("bookManage", bookManage);
        return "viewBook";
    }

    @GetMapping("/toBook")
    public String toBook(Integer id, Model model) {
        BookManage bookManage = bookManageService.getByIdBiz(id);
        model.addAttribute("bookManage", bookManage);
        return "updateBook";
    }

    @PostMapping("/updateBook")
    public String updateBook(BookManage bookManage) {
        int updateBiz = bookManageService.updateBiz(bookManage);
        if (updateBiz > 0) {
            return "redirect:/bookManage/listBook";
        } else {
            return "redirect:/error.jsp";
        }
    }
}
